#__*__coeding:utf-8__*__
#!/usr/share/python
'''
---------------------------------------------------------------- 
 版权所有。  
 
 文件名：
 文件功能描述： 
 author：
 时间：
--------------------------------------------------------------- 
'''

import gzip
import os
import re
import shutil

from Base.config import conf
from Base.tools import tools
from Base.txt_io import IO


class log_dispose:


    def __init__(self):
        self.flag='log_path'
        _path=conf.get_conf(conf,self.flag)
        self.path=_path.replace('\n',tools.charte())

    def get_filename_alllist(self):
        '''
        According to the path for the log file directory within all file names
        :return: all file names <type:list>
        '''
        new_path=self.path.replace('\n',tools.charte())
        name_list=IO.listdir(new_path)
        return name_list

    def need_date(self,name_list):
        '''
        剔除传入文件名字别表不匹配需处理文件名规则的项
        :param name_list:
        :return: 剔除不需要文件名后的列表  属性：列表
        '''
        need_name_list=[]
        for i in name_list:
            if self.FilenameFiltering(i):
                need_name_list.append(i)
        if len(need_name_list)>1:
            return need_name_list
        else: return None


    def FilenameFiltering(self,filename):
        if re.match('(?P<filename>access.log-)+(?P<date>\d{4}|[1-9999]{2}|[01-12]{2}|[01-31])+(?P<suffix>.\.gz$)', filename):
            return True
        else: return False

    def move_old_file(self,file_name):
        '''
        The incoming file using the tool module directory of processing method on the path to wake
        :param: file name
        :return: True or Flase
        '''
        new_dir = 'processed_log'
        old_paths=str(self.path)
        old_path=old_paths.strip('\n')
        old_file_path = tools.path_combination(old_path, file_name)
        moeve_path=tools.path_combination(old_path,new_dir)
        try:
            if tools.is_directory(moeve_path):
                new_dir_path =tools.path_combination(old_path, new_dir)
                os.makedirs(new_dir_path)
                shutil.move(old_file_path, moeve_path)
            else:
                shutil.move(old_file_path, moeve_path)
            return True
        except shutil.Error:
            os.remove(old_file_path)

    def log_date_re(self,string):
        '''
        According to the date of the incoming string extraction of Numbers。
        :param string: 带日期格式字符串
        :return: 日期字符串 属性：str
        '''
        return re.sub(r'\D','',string)

    #日志基础处理
    def baselogdispose(self,file_name):
        '''
        1. The use of the incoming file name list circulating reading list in the log file content.
        2. Split the log content
        3.
        :param file_name: file name list
        :return:To dictionary to assemble all the IP list, access time, request the address list,
        proxy address list, user access in the form of software features, user equipment list
        <type: dict>
        '''

        if self.FilenameFiltering(file_name): #匹配日志文件名
            log_date=tools.str_to_date(self.log_date_re(file_name))
            allcunt = 0
            remote_addr = []
            time_local = []
            request = []
            http_referer = []
            http_user_agent = []
            http_x_forwarded_for = []
            relfile=gzip.open('%s%s' % (self.path, file_name), 'r')
            for j in relfile:
                logfile=str(j,encoding='utf-8')
                log = logfile.strip('\n').split(' ')
                allcunt += 1        #统计当前文件（当日）访问总记录数
                remote_addr.append(log[0])
                if len(log) > 4:
                    time_local.append(log[3].strip('[')[0:14])
                else:
                    time_local.append('null')
                if len(log) > 11:
                    request.append(log[6])
                else:
                    request.append('null')
                if len(log) > 11:
                    http_referer.append(log[10])
                else:
                    http_referer.append('null')
                if len(log) > 11:
                    http_user_agent.append(log[11])
                else:
                    http_user_agent.append('null')
                if len(log) > 15:
                    http_x_forwarded_for.append(
                    log[12].strip('()') + log[13] + log[14] + log[15].strip('()'))
                else:
                    http_x_forwarded_for.append('null')
                baselog = {
                    'allcunt': allcunt,
                    'IP': remote_addr,
                    'Access_time': time_local,
                    'Access_addr': request,
                    'Access_referer_addr': http_referer,
                    'Access_agent': http_user_agent,
                    'Access_forwarded_for': http_x_forwarded_for,
                    'log_date': log_date
                    }
            relfile.close()
            return baselog